﻿using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Audio;
using UnityEngine.Events;
using UnityEngine.Sprites;
using UnityEngine.Scripting;
using UnityEngine.Assertions;
using UnityEngine.EventSystems;
using UnityEngine.Assertions.Must;
using UnityEngine.Assertions.Comparers;
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEditor;

namespace CommonWidget
{
    public class ButtonCreater : DefultElementCreater
    {
        protected override string MenuPath
        {
            get
            {
                return "GameObject/UI/Button";
            }
        }
        protected override Type CommponentType
        {
            get
            {
                return typeof(Button);
            }
        }
        protected override void ChargeWidgetInfo(UnityEngine.Component component, WidgetItem info)
        {
            var btn = component as Button;
            var spriteDic = info.spriteDic;
            var colorDic = info.colorDic;

            if (spriteDic.ContainsKey(KeyWord.normal))
            {
                var texture = spriteDic[KeyWord.normal];

                var image = btn.targetGraphic as Image;
                image.sprite = texture;
                image.type = Image.Type.Simple;
                WidgetUtility.SetNativeSize(image);
            }

            if (colorDic.ContainsKey(KeyWord.titleColor))
            {
                var color = colorDic[KeyWord.titleColor];
                var text = btn.GetComponentInChildren<Text>();
                text.color = color;
            }

            foreach (var item in colorDic)
            {
                btn.transition = Selectable.Transition.ColorTint;

                var keyword = item.Key;
                var color = item.Value;

                var colors = btn.colors;
                if (keyword == KeyWord.normal)
                {
                    var image = btn.targetGraphic as Image;
                    image.color = color;
                    colors.normalColor = color;
                    Debug.Log(item.Value);
                }

                if (keyword == KeyWord.disabled)
                {
                    colors.disabledColor = color;
                }
                else if (keyword == KeyWord.highlighted)
                {
                    colors.highlightedColor = color;
                }
                else if (keyword == KeyWord.pressed)
                {
                    colors.pressedColor = color;
                }
                btn.colors = colors;
            }

            foreach (var item in spriteDic)
            {
                if (item.Key == KeyWord.normal) continue;

                btn.transition = Selectable.Transition.SpriteSwap;
                var keyword = item.Key;
                var sprite = item.Value;
                var state = btn.spriteState;
                if (keyword == KeyWord.disabled)
                {
                    state.disabledSprite = sprite;
                }
                else if (keyword == KeyWord.highlighted)
                {
                    state.highlightedSprite = sprite;
                }
                else if (keyword == KeyWord.pressed)
                {
                    state.pressedSprite = sprite;
                }
                btn.spriteState = state;
            }
        }

        public override List<Sprite> GetPreviewList(WidgetItem info)
        {
            List<Sprite> List = new List<Sprite>();
            var spriteDic = info.spriteDic;
            var colorDic = info.colorDic;
            if (spriteDic.ContainsKey(KeyWord.normal))
            {
                var sprite = spriteDic[KeyWord.normal];
                List.Add(sprite);
            }
            else if(colorDic.ContainsKey(KeyWord.normal))
            {
                var texture = new Texture2D(60,30);
                for (int i = 0; i < texture.width; i++)
                {
                    for (int j = 0; j < texture.height; j++)
                    {
                        texture.SetPixel(i, j, colorDic[KeyWord.normal]);
                    }
                }
                texture.Apply();
                List.Add(Sprite.Create(texture,new Rect(0,0,texture.width,texture.height),new Vector2(texture.width,texture.height) * 0.5f));
            }
            return List;
        }
        protected override List<string> CreateSpriteList()
        {
            return new List<string>() { KeyWord.normal, KeyWord.pressed, KeyWord.highlighted, KeyWord.disabled };
        }
        protected override List<string> CreateColorList()
        {
            var colors = CreateSpriteList();
            colors.Add(KeyWord.titleColor);
            return colors;
        }
    }
}